<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>三亚之旅</title>
    <style>
        body{
            background-color: blanchedalmond;
            padding: 0 50px;
        }

        img{
            width: 100%;
            height: 450px;
        }

        button{
            background-color: hotpink;
            width: 70px;
            height: 40px;
        }

        #box{
            width: 600px;
            margin: 100px auto;
            text-align: center;
        }
    </style>
</head>
<body>
    <div id="box">
        <img id="icon" src="imgs/1.jpg" alt="">
        <div>
            <button id="prev">上一张</button>
            <button id="next">下一张</button>
        </div>
    </div>

    <script>
        window.onload = function () {
            //1.获取需要的标签
            let icon = document.getElementById('icon');
            let prev = document.getElementById('prev');
            let next = document.getElementById('next');

            let currentIndex = 1 , minIndex = 1 , maxIndex = 21;

            //2.监听按钮点击
            //2.1上一张
            prev.onclick = function () {
                if (currentIndex === minIndex) { //到达最小边界 
                   currentIndex = maxIndex;
                
               }else {
                    currentIndex--;
               }
               icon.src =`imgs/${currentIndex}.jpg`;
            };
            


            //2.2下一张
            next.onclick = function () {
               if (currentIndex === maxIndex) { //到达最大边界 
                   currentIndex = minIndex;
                
               }else {
                    currentIndex++;
               }
               icon.src =`imgs/${currentIndex}.jpg`;
            };





        }
        
    </script>
</body>
</html>